package com.itheima.service.impl;

import com.itheima.dao.OrderSettingDao;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.*;

@Service
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    private OrderSettingDao orderSettingDao;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void doImport(List<OrderSetting> osList) {
        //1. 非空判断
        if (!CollectionUtils.isEmpty(osList)) {
            //2. 遍历
            for (OrderSetting orderSetting : osList) {
                //3. 获取导入的设置信息的日期
                Date orderDate = orderSetting.getOrderDate();
                //4. 通过日期查询预约设置表
                OrderSetting osInDB = orderSettingDao.findByOrderDate(orderDate);
                //5. 如果存在记录
                if (null != osInDB) {
                    //5.1 取数据库中的已预约人数
                    int reservations = osInDB.getReservations();
                    //5.2 取出要更新的最大预约数
                    int number = orderSetting.getNumber();
                    //5.3 如果已人数>要更新的最大预约数，报错
                    if (reservations > number) {
                        throw new MyException("最大预约数不能小于已预约人数!");
                    }
                    //5.4 否则可以更新最大预约数
                    orderSettingDao.updateNumber(orderSetting);
                } else {
                    //6. 如果不存在
                    //7. 添加新记录
                    orderSettingDao.add(orderSetting);
                }
            }
        }
        //8. 事务控制
    }


    @Override
    public List<Map> getOrderSettingByMonth(String date) {
        // 1.组织查询Map，dateBegin表示月份开始时间，dateEnd月份结束时间
        String dateBegin = date + "-1";//2019-03-1
        String dateEnd = date + "-31";//2019-03-31
        Map map = new HashMap();
        map.put("dateBegin", dateBegin);
        map.put("dateEnd", dateEnd);
        // 2.查询当前月份的预约设置
        List<OrderSetting> list = orderSettingDao.getOrderSettingByMonth(map);
        List<Map> data = new ArrayList<>();
        // 3.将List<OrderSetting>，组织成List<Map>
        for (OrderSetting orderSetting : list) {
            Map orderSettingMap = new HashMap();
            orderSettingMap.put("date", orderSetting.getOrderDate().getDate());//获得日期（几号）
            orderSettingMap.put("number", orderSetting.getNumber());//可预约人数
            orderSettingMap.put("reservations", orderSetting.getReservations());//已预约人数
            data.add(orderSettingMap);
        }
        return data;
    }

    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        Date orderDate = orderSetting.getOrderDate();
        long low = orderSettingDao.findCountByOrderDate(orderDate);

       if (low>0){
           orderSettingDao.editNumberByOrderDate(orderSetting);
       }else {
           orderSettingDao.add(orderSetting);
       }
    }
}
